package com.example.prjtimerservice;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ActMain extends Activity {

    private Button btnService;
    private TextView textView;

    private final View.OnClickListener 
    btnService_Click = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ActMain.this, MyService.class);
            startService(intent);
            btnService.setEnabled(false);
        }
    };
    
    private BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Long second = Long.parseLong(bundle.getString(MyService.SECOND));
                textView.setText("second: " + second);
                
                if (second == 0) {
                    //Toast.makeText(ActMain.this, "倒數計時完成", Toast.LENGTH_LONG).show();
                    btnService.setEnabled(true);
                }
            }
        }
    };
    
    private void initializeComponent() {
        btnService= (Button) findViewById(R.id.btnService);
        btnService.setOnClickListener(btnService_Click);
        
        textView= (TextView) findViewById(R.id.textView1);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actmain);
        initializeComponent();
        registerReceiver(receiver, new IntentFilter(MyService.NOTIFICATION));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.act_main, menu);
        return true;
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        //registerReceiver(receiver, new IntentFilter(MyService.NOTIFICATION));
    }

    @Override
    protected void onPause() {
        super.onPause();
        //unregisterReceiver(receiver);
    }
    
}
